//
//  Users.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "Users.h"
#import "NSString+Path.h"
#import "NSObject+CZRuntime.h"
#import "HMDBManager.h"

@implementation Users


+ (instancetype)userWithDic:(NSDictionary *)dic{
    
    Users *user = [[Users alloc] init];
    
    user.true_name = dic[@"true_name"];
    
    [user setValuesForKeysWithDictionary:dic];
    
    return user;
}

- (NSString *)description
{
    return [self yy_modelDescription];
}

//插入Person
- (BOOL)insertUser
{
    
    __block  NSString *sql;
    [self getSqlStringListCompleteBolck:^(NSString *propertyStr, NSString *valueStr, NSString *updateSqlStr) {
        sql = [NSString stringWithFormat:@"insert into t_user (%@) values (%@)",propertyStr,valueStr];
    }];
    
    return  [[HMDBManager sharedDBManager] execSqlWithSqlString:sql];
}

//更新,修改Person
- (BOOL)updateUser
{
    
    
//    NSString *sql = [NSString stringWithFormat:@"update t_person set name = '%@',age = %d where pid = %d",self.name,self.age.intValue,self.pid.intValue];
    
    __block NSMutableString *sql = [NSMutableString stringWithFormat:@"update t_user set "];
    
    [self getSqlStringListCompleteBolck:^(NSString *propertyStr, NSString *valueStr, NSString *updateSqlStr) {
        
        [sql appendString:updateSqlStr];
        
        
    }];
    
    [sql appendFormat:@" where pid = %d",[self.pid intValue]]; 
    
    return  [[HMDBManager sharedDBManager] execSqlWithSqlString:sql];
}

//删除Person
- (BOOL)deleteUser
{
    NSString *sql = [NSString stringWithFormat:@"delete form t_user where pid = %d",self.pid.intValue];
    
    return [[HMDBManager sharedDBManager] execSqlWithSqlString:sql];
}

//查询库中所有Person的集合
- (NSArray *)qureyUsers
{
    return nil;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}

- (void)getSqlStringListCompleteBolck :(void(^)(NSString *propertyStr,NSString *valueStr,NSString *updateSqlStr))completeBlock
{
   
    
    NSArray *propertiesList =  [Users cz_propertiesList];
    
    NSMutableArray *valueList = [NSMutableArray array];
    NSMutableArray *keyList = [NSMutableArray array];
    
    [propertiesList enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        id value = [self valueForKey:obj];
        
        if (value)
        {
            [valueList addObject:value];
            [keyList addObject:obj];
        }
    
    }];
    
    //
    
    NSMutableString *kStrM = [NSMutableString string];
    NSMutableString *valueString = [NSMutableString string];
    NSMutableString *updateSqlStr = [NSMutableString string];
    for (int i = 0; i < keyList.count; i++)
    {
        id value = valueList[i];
        NSString *keyStr = keyList[i];
        
        if (i != keyList.count -1)
        {
            [kStrM appendFormat:@"%@,",keyStr];
            
        }
        else
        {
            [kStrM appendFormat:@"%@",keyStr];
        
        }
        
        if ([value isKindOfClass:[NSString class]])
        {
            
            if (i == keyList.count - 1)
            {
                [valueString appendFormat:@"'%@'",value];
//                name = '%@',age = %d
                if (![keyStr isEqualToString:@"pid"]) {
                    [updateSqlStr appendFormat:@"%@ = '%@'",keyStr,value];
                }
                
            }
            else
            {
                [valueString appendFormat:@"'%@',",value];
                if (![keyStr isEqualToString:@"pid"]) {
                    [updateSqlStr appendFormat:@"%@ = '%@',",keyStr,value];
                }
                
            }
        }
        else
        {
            if (i == keyList.count - 1) {
                [valueString appendFormat:@"%d",[value intValue]];
                if (![keyStr isEqualToString:@"pid"]) {
                 [updateSqlStr appendFormat:@"%@ = %d",keyStr,[value intValue]];
                }
            }
            else
            {
                [valueString appendFormat:@"%d,",[value intValue]];
                if (![keyStr isEqualToString:@"pid"]) {
                [updateSqlStr appendFormat:@"%@ = %d,",keyStr,[value intValue]];
                }
            }
            
        }
    }
    
    completeBlock(kStrM,valueString,updateSqlStr);
}

/*
 

@property (nonatomic, copy) NSString *gender;


@property (nonatomic, copy) NSString *true_name;

@property (nonatomic, copy) NSNumber *mobile_number;


@property (nonatomic, copy) NSNumber *province_id;


@property (nonatomic, copy) NSNumber *county_id;


@property (nonatomic, copy) NSNumber *age;

@property (nonatomic, copy) NSNumber *user_id;


@property (nonatomic, copy) NSNumber *weight;


@property (nonatomic, copy) NSNumber *city_id;


@property (nonatomic, copy) NSNumber *is_certify;

@property (nonatomic, copy) NSString *address;

@property (nonatomic, copy) NSNumber *card_type;

@property (nonatomic, copy) NSString *easymob_password;

@property (nonatomic, copy) NSString *login_token;

@property (nonatomic, copy) NSString *head_photo;

@property (nonatomic, copy) NSNumber *card_number;

@property (nonatomic, copy) NSNumber *height;
 */

//归档  告诉系统,该对象有哪些属性需要存储
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.gender forKey:@"gender"];
    [aCoder encodeObject:self.true_name forKey:@"true_name"];
    [aCoder encodeObject:self.mobile_number forKey:@"mobile_number"];
    [aCoder encodeObject:self.province_id forKey:@"province_id"];
    [aCoder encodeObject:self.county_id forKey:@"county_id"];
    [aCoder encodeObject:self.age forKey:@"age"];
    [aCoder encodeObject:self.user_id forKey:@"user_id"];
    [aCoder encodeObject:self.weight forKey:@"weight"];
    [aCoder encodeObject:self.city_id forKey:@"city_id"];
    [aCoder encodeObject:self.is_certify forKey:@"is_certify"];
    [aCoder encodeObject:self.address forKey:@"address"];
    [aCoder encodeObject:self.card_type forKey:@"card_type"];
    [aCoder encodeObject:self.easymob_password forKey:@"easymob_password"];
    [aCoder encodeObject:self.login_token forKey:@"login_token"];
    [aCoder encodeObject:self.head_photo forKey:@"head_photo"];
    [aCoder encodeObject:self.card_number forKey:@"card_number"];
    [aCoder encodeObject:self.height forKey:@"height"];
      [aCoder encodeObject:self.account forKey:@"account"];
    
}


//解档   告诉系统,该对象有哪些属性可以取
-(nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if ([super init]) {
        self.gender = [aDecoder decodeObjectForKey:@"gender"];
        self.true_name = [aDecoder decodeObjectForKey:@"true_name"];
        self.mobile_number = [aDecoder decodeObjectForKey:@"mobile_number"];
        self.province_id = [aDecoder decodeObjectForKey:@"province_id"];
        self.county_id = [aDecoder decodeObjectForKey:@"county_id"];
        self.age = [aDecoder decodeObjectForKey:@"age"];
        self.user_id = [aDecoder decodeObjectForKey:@"user_id"];
        self.weight = [aDecoder decodeObjectForKey:@"weight"];
        self.city_id = [aDecoder decodeObjectForKey:@"city_id"];
        self.is_certify = [aDecoder decodeObjectForKey:@"is_certify"];
        self.address = [aDecoder decodeObjectForKey:@"address"];
        self.card_type = [aDecoder decodeObjectForKey:@"card_type"];
        self.easymob_password = [aDecoder decodeObjectForKey:@"easymob_password"];
        self.login_token = [aDecoder decodeObjectForKey:@"login_token"];
        self.head_photo = [aDecoder decodeObjectForKey:@"head_photo"];
        self.card_number = [aDecoder decodeObjectForKey:@"card_number"];
        self.height = [aDecoder decodeObjectForKey:@"height"];
        self.account = [aDecoder decodeObjectForKey:@"account"];
        
    }
    return self;
}




@end
